package ui.views.property;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import atempdao.EgnPortfolioDao;

import ui.util.IStructuredViewer;
import dao.EgnPortfolio;
import de.ralfebert.rcputils.builder.table.ColumnBuilder;
import de.ralfebert.rcputils.builder.table.TableViewerBuilder;
import de.ralfebert.rcputils.builder.table.format.Formatter;

public class PortfolioDefineSection extends AbstractPropertySection implements IStructuredViewer{
	private final static Logger logger = LoggerFactory.getLogger(PortfolioDefineSection.class);

	private EgnPortfolio port;
	private TableViewer tableViewer;
	
	@Override
	public TableViewer getTableViewer() {
		return tableViewer;
	}
	@Override
	public TreeViewer getTreeViewer() {
		return null;
	}
	
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		if( input instanceof EgnPortfolio ){
			List<EgnPortfolio> portfolios =new ArrayList<EgnPortfolio>();
			this.port = (EgnPortfolio) input;
			portfolios.add(0,port);
			portfolios.addAll(EgnPortfolioDao.getChildEgnPortfolio(port.getPortfolioId()));
			tableViewer.setInput(portfolios);
			
		}
		else tableViewer.setInput(EgnPortfolioDao.getRootEngPortfolio());
		
		Formatter.setZebraBackground(tableViewer);
	}

	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		createTable(parent);
		
	}
//	@Override
//	public void refresh() {
		// labelText.removeModifyListener(listener);
		// ButtonElementProperties properties = (ButtonElementProperties)
		// buttonElement
		// .getAdapter(IPropertySource.class);
		// labelText.setText(properties.strText);
		// labelText.addModifyListener(listener);
//	}
	
	private void createTable(Composite parent){
		TableViewerBuilder t = new TableViewerBuilder(parent);

		ColumnBuilder id = t.createColumn("Id");
		id.bindToProperty("portfolioId");
		id.useAsDefaultSortColumn();
		id.build();
		
		ColumnBuilder type = t.createColumn("Type");
		type.bindToProperty("portfolioType");
		type.build();
		
		ColumnBuilder key1 = t.createColumn("Key1");
		key1.bindToProperty("compositeKey1");
		key1.build();
		
		ColumnBuilder key2 = t.createColumn("Key2");
		key2.bindToProperty("compositeKey2");
		key2.build();
		
		ColumnBuilder key3 = t.createColumn("Key3");
		key3.bindToProperty("compositeKey3");
		key3.build();
		
		ColumnBuilder key4 = t.createColumn("Key4");
		key4.bindToProperty("compositeKey4");
		key4.build();
		
		ColumnBuilder key5 = t.createColumn("Key5");
		key5.bindToProperty("compositeKey5");
		key5.build();

		t.setInput(null);
		tableViewer = t.getTableViewer();
		
//		GridData gridData =new GridData();
//		gridData.grabExcessVerticalSpace =true;
//		tableViewer.getTable().setLayoutData(gridData);
	}
}
